package org.intrasearch.core.io;

import java.io.Externalizable;
import java.util.*;

public class CachingSerializationConfig implements SerializationConfig
{
    private Map<Class<Object>, ExternalizableProvider<Object>> providers = new HashMap<Class<Object>, ExternalizableProvider<Object>>();

    public CachingSerializationConfig(SerializationConfig serializationConfig)
    {
        serializationConfig.configure(new Builder());
    }

    public void configure(SerializationBuilder builder)
    {
        for (Map.Entry<Class<Object>,ExternalizableProvider<Object>> classExternalizableProviderEntry : providers.entrySet())
        {
            builder.addExternalizableProvider(classExternalizableProviderEntry.getKey(), classExternalizableProviderEntry.getValue());
        }
    }

    private class Builder implements SerializationBuilder
    {
        public void addClass(Class<? extends Externalizable> externalizableClass)
        {
            addExternalizableProvider(externalizableClass, new DefaultExternalizableProvider<Externalizable>(externalizableClass));
        }

        public <T> void addExternalizableProvider(Class<? extends T> aClass, ExternalizableProvider<T> externalizableProvider)
        {
            providers.put((Class<Object>)aClass, (ExternalizableProvider<Object>)externalizableProvider);
        }
    }
}
